
package com.wyzz.hr.action;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpSession;
import org.apache.axis.utils.StringUtils;
import org.apache.commons.beanutils.BeanUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IEmpAddConfBo;
import com.wyzz.hr.bo.IEmpAdditionalBo;
import com.wyzz.hr.bo.IEmpEduHisBo;
import com.wyzz.hr.bo.IEmpJobHisBo;
import com.wyzz.hr.bo.IEmpRelationsBo;
import com.wyzz.hr.bo.IEmpTrainHisBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IEmpregistryBO;
import com.wyzz.hr.pojo.Empaddconf;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.util.MyTools;

public class EmpAdditional extends BaseAction {

	public EmpAdditional() {
	}

	public String listAll() throws Exception {
		if (StringUtils.isEmpty(empNo) || empNo.equals("null"))
			empNo = getCurrentEmpNo();
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		employee = empBo.loadEmp(empNo, null);
		if (employee == null)
			return "noemp";
		empName = employee.getEmpName();
		if (!empNo.equals(getCurrentEmpNo())) {
			if ("OWN".equals(super.authorityCondition))
				return "noauth";
			if (("SUB".equals(super.authorityCondition) || "HRSUB"
					.equals(super.authorityCondition))
					&& !checkEmpInCharge(employee, 1))
				return "noauth";
		}
		IEmpJobHisBo empJobHisBo = (IEmpJobHisBo) BaseAction
				.getBean("empJobHisBo");
		ehjList = empJobHisBo.search(empNo);
		IEmpEduHisBo empEduHisBo = (IEmpEduHisBo) BaseAction
				.getBean("empEduHisBo");
		eheList = empEduHisBo.search(empNo);
		IEmpTrainHisBo empTrainHisBo = (IEmpTrainHisBo) BaseAction
				.getBean("empTrainHisBo");
		ehtList = empTrainHisBo.search(empNo);
		IEmpRelationsBo empRelationsBo = (IEmpRelationsBo) BaseAction
				.getBean("empRelationsBo");
		erelList = empRelationsBo.search(empNo);
		IEmpregistryBO empRegistryBO = (IEmpregistryBO) getBean("empRegistryBO");
		eregList = empRegistryBO.loadByEmp(empNo);
		IEmpAddConfBo empAddConfBo = (IEmpAddConfBo) BaseAction
				.getBean("empAddConfBo");
		empaddconfList = empAddConfBo.listByTable("empAdditional");
		String resume = employee.getEmpResume1();
		if (resume != null)
			resume1DownLoadName = (new StringBuilder()).append(
					"\u4E2D\u6587\u7B80\u5386").append(
					resume.substring(resume.lastIndexOf('.'))).toString();
		resume = employee.getEmpResume2();
		if (resume != null)
			resume2DownLoadName = (new StringBuilder()).append(
					"\u82F1\u6587\u7B80\u5386").append(
					resume.substring(resume.lastIndexOf('.'))).toString();
		int size = empaddconfList.size();
		for (int i = 0; i < size; i++) {
			Empaddconf eac = (Empaddconf) empaddconfList.get(i);
			int index = eac.getEadcSeq().intValue();
			eac.setFieldName((new StringBuilder()).append("empAdditional")
					.append(index).toString());
			BeanUtils.setProperty(eac, "value", BeanUtils.getProperty(employee,
					(new StringBuilder()).append("empAdditional").append(index)
							.toString()));
			if ("select".equals(eac.getEadcFieldType()))
				eac.setFieldValueList(Arrays.asList(eac.getEadcFieldValue()
						.split(",")));
		}

		return "success";
	}

	public String resumeUpload() throws Exception {
		if ((filecFileName == null || filecFileName.equals(""))
				&& (fileeFileName == null || fileeFileName.equals("")))
			return null;
		String pathConfig;
		String typeConfig;
		String lengthConfig;
		String resume1;
		String resume2;
		if (!empNo.equals(getCurrentEmpNo())
				&& "SUB".equals(super.authorityCondition) && !checkAuth(empNo)) {
			addActionError("\u60A8\u6CA1\u6709\u65B0\u589E\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");// 您没有新增权限执行本操作
			// ！
			return "error";
		}
		if (empNo == null || empNo.trim().length() == 0)
			empNo = getCurrentEmpNo();
		pathConfig = "sys.profile.file.path";
		typeConfig = "sys.profile.file.type";
		lengthConfig = "sys.profile.file.length";
		resume1 = null;
		resume2 = null;
		String UploadResult;
		if (filecFileName != null && !"".equals(filecFileName)) {
			String postfix = filecFileName.substring(filecFileName
					.lastIndexOf("."));
			filecFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(filec, pathConfig, typeConfig,
					lengthConfig, new String[] { filecFileName });
			if ("property".equals(UploadResult)) {
				addActionError("\u4E2D\u6587\u7B80\u5386\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");// 中文简历的上传错误
				// ！
				return "error";
			}
			try {
				if ("fileExtendNameError".equals(UploadResult)) {
					addActionError("\u4E2D\u6587\u7B80\u5386\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01"); // 中文简历的后缀名不合法
					// ！
					return "error";
				}
			} catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
			resume1 = filecFileName;
		}
		if (fileeFileName != null && !"".equals(fileeFileName)) {
			String postfix = fileeFileName.substring(fileeFileName
					.lastIndexOf("."));
			fileeFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(filee, pathConfig, typeConfig,
					lengthConfig, new String[] { fileeFileName });
			if ("property".equals(UploadResult)) {
				addActionError("\u82F1\u6587\u7B80\u5386\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");// 英文简历的上传错误
				// ！
				return "error";
			}
			if ("fileExtendNameError".equals(UploadResult)) {
				addActionError("\u82F1\u6587\u7B80\u5386\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");// 英文简历的后缀名不合法
				// ！
				return "error";
			}
			resume2 = fileeFileName;
		}
		IEmpAdditionalBo empAddtionalBo = (IEmpAdditionalBo) getBean("empAdditionalBo");
		empAddtionalBo.saveResume(resume1, resume2, empNo);
		addActionMessage("\u7B80\u5386\u6587\u4EF6\u4E0A\u4F20\u6210\u529F");// 简历文件上传成功
		return "success";
	}

	public String resumeDelete() throws Exception {
		if ("SUB".equals(super.authorityCondition) && !checkAuth(empNo)) {
			addActionError("\u60A8\u6CA1\u6709\u65B0\u589E\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		if (empNo == null || empNo.equals(""))
			empNo = (String) getSession().getAttribute("curr_oper_no");
		if (empNo == null || empNo.trim().length() == 0)
			empNo = getCurrentEmpNo();
		IEmpAdditionalBo empAdditionalBo = (IEmpAdditionalBo) getBean("empAdditionalBo");
		if (filecFileName != null) {
			FileOperate.deleteFile("sys.profile.file.path", filecFileName);
			empAdditionalBo.deleteResume(empNo, 1);
		}
		if (fileeFileName != null) {
			FileOperate.deleteFile("sys.profile.file.path", fileeFileName);
			empAdditionalBo.deleteResume(empNo, 2);
		}
		addActionMessage("\u7B80\u5386\u5220\u9664\u6210\u529F\u3002");
		return "success";
	}

	public String updateAdditional(Employee emp, String empNo) throws Exception {
		if (empNo == null || empNo.equals(""))
			empNo = (String) getSession().getAttribute("curr_oper_no");
		if (empNo == null || empNo.trim().length() == 0)
			empNo = getCurrentEmpNo();
		if (empNo == "self")
			empNo = getCurrentEmpNo();
		String auth = DWRUtil.checkAuth("empAdditionaldwr", "");
		if ("SUB".equals(auth) && !checkAuth(empNo)) {
			addActionError("\u60A8\u6CA1\u6709\u589E\u52A0\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		} else {
			IEmpAdditionalBo empAddtionalBo = (IEmpAdditionalBo) getBean("empAdditionalBo");
			empAddtionalBo.save(emp, empNo);
			return "success";
		}
	}

	public String getWorkOrJoinYear(String dateStr) {
		String result = "";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = format.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Float temp = MyTools.getWorkYearProfile(date, new Date());
		result = getYearOrMonth(temp);
		return result;
	}

	public String getYearOrMonth(Float joinYear) {
		int value = 0;
		String result = "";
		if (joinYear.floatValue() >= 1.0F) {
			value = Integer.parseInt((new StringBuilder()).append(
					joinYear.intValue()).append("").toString());
			result = (new StringBuilder()).append(result).append(value).append(
					"\u5E74").toString();
		} else {
			joinYear = Float.valueOf(joinYear.floatValue() * 100F);
			value = Integer.parseInt((new StringBuilder()).append(
					joinYear.intValue()).append("").toString());
			result = (new StringBuilder()).append(result).append(value).append(
					"\u4E2A\u6708").toString();
		}
		return result;
	}

	public List getEhjList() {
		return ehjList;
	}

	public void setEhjList(List ehjList) {
		this.ehjList = ehjList;
	}

	public List getEheList() {
		return eheList;
	}

	public void setEheList(List eheList) {
		this.eheList = eheList;
	}

	public List getEhtList() {
		return ehtList;
	}

	public void setEhtList(List ehtList) {
		this.ehtList = ehtList;
	}

	public List getEmpaddconfList() {
		return empaddconfList;
	}

	public void setEmpaddconfList(List empaddconfList) {
		this.empaddconfList = empaddconfList;
	}

	public String getEmpNo() {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getFilecFileName() {
		return filecFileName;
	}

	public void setFilecFileName(String filecFileName) {
		this.filecFileName = filecFileName;
	}

	public File getFilec() {
		return filec;
	}

	public void setFilec(File filec) {
		this.filec = filec;
	}

	public String getFileeFileName() {
		return fileeFileName;
	}

	public void setFileeFileName(String fileeFileName) {
		this.fileeFileName = fileeFileName;
	}

	public File getFilee() {
		return filee;
	}

	public void setFilee(File filee) {
		this.filee = filee;
	}

	public String getResume1DownLoadName() {
		return resume1DownLoadName;
	}

	public void setResume1DownLoadName(String resume1DownLoadName) {
		this.resume1DownLoadName = resume1DownLoadName;
	}

	public String getResume2DownLoadName() {
		return resume2DownLoadName;
	}

	public void setResume2DownLoadName(String resume2DownLoadName) {
		this.resume2DownLoadName = resume2DownLoadName;
	}

	public List getErelList() {
		return erelList;
	}

	public void setErelList(List erelList) {
		this.erelList = erelList;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public String getFilecContentType() {
		return filecContentType;
	}

	public void setFilecContentType(String filecContentType) {
		this.filecContentType = filecContentType;
	}

	public String getFileeContentType() {
		return fileeContentType;
	}

	public void setFileeContentType(String fileeContentType) {
		this.fileeContentType = fileeContentType;
	}

	public List getEregList() {
		return eregList;
	}

	public void setEregList(List eregList) {
		this.eregList = eregList;
	}

	private static final long serialVersionUID = 1L;
	List ehjList;
	List eheList;
	List ehtList;
	List erelList;
	List eregList;
	private String filecFileName;
	private File filec;
	private String filecContentType;
	private String fileeFileName;
	private File filee;
	private String fileeContentType;
	private List empaddconfList;
	private String empNo;
	private String empName;
	private String resume1DownLoadName;
	private String resume2DownLoadName;
	private Employee employee;
}
